package com.the9tcat.deliverycoming.component;


import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.the9tcat.deliverycoming.R;
import com.the9tcat.deliverycoming.modle.DeliveryInfo;
import com.the9tcat.deliverycoming.util.Logger;

/**
 * 
 * @author       Aliusa 
 * @class        DeliveryListViewAdapter 
 * @description  
 * @notes        
 * @copyright    2011-8-7 ����11:48:40. All rights reserved.
 */
public class DeliveryAdapter extends CursorAdapter {
    
    private LayoutInflater mInflater;
    
    /**
     * 
     * @author       Aliusa 
     * @class        ViewHolder 
     * @description  
     * @notes        
     * @copyright    2011-8-7 ����11:23:07. All rights reserved.
     */
    class ViewHolder {
        
        private ImageView item_signed;
        private LinearLayout bg_title;
        private LinearLayout bg_content;
        private TextView item_name;
        private TextView item_expressCompany;
        private TextView item_trackingNumber;
        private TextView item_trackingTime;
        private TextView item_trackingInfor;
        
    }

    /**
     * 
     * @param context
     * @param c
     */
    public DeliveryAdapter(Context context, Cursor c) {
        super(context, c, true);
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
    }



    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    /**
     * *********************************************************
     * @see android.widget.CursorAdapter#getView(int, android.view.View, android.view.ViewGroup)
     * @function    getView
     * @param position
     * @param convertView
     * @param parent
     * @return      
     * @description 
     * @notes       
     * @copyright   2011-8-7 11:23:42. All rights reserved.
     ***********************************************************
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getCursor().moveToPosition(position))
        {
            final ViewHolder holder;
            if (null == convertView){
                convertView = mInflater.inflate(R.layout.delivery_item, null);
                holder = new ViewHolder();
                holder.item_signed = (ImageView)convertView.findViewById(R.id.item_signed);
                holder.bg_title = (LinearLayout)convertView.findViewById(R.id.bg_title);
                holder.bg_content = (LinearLayout)convertView.findViewById(R.id.bg_content);
                holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
                holder.item_expressCompany = (TextView) convertView.findViewById(R.id.item_expressCompany);
                holder.item_trackingNumber = (TextView) convertView.findViewById(R.id.item_trackingNumber);
                holder.item_trackingTime = (TextView) convertView.findViewById(R.id.item_trackingTime);
                holder.item_trackingInfor = (TextView) convertView.findViewById(R.id.item_trackingInfor);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            DeliveryInfo delivery = new DeliveryInfo(getCursor(), position);
            Logger.d("DeliveryComing", "name="+delivery.getName());
            if (delivery.isSigned()){
                
                holder.item_signed.setVisibility(View.VISIBLE);
                
            }else{
                holder.item_signed.setVisibility(View.GONE);
                
                final int color_id = delivery.getColor();
                switch(color_id){
                    case DeliveryInfo.COLOR_BLUE:
                        holder.bg_title.setBackgroundResource(R.drawable.bg_title_blue);
                        holder.bg_content.setBackgroundResource(R.drawable.bg_content_blue);
                        break;
                    case DeliveryInfo.COLOR_YELLOW:
                        holder.bg_title.setBackgroundResource(R.drawable.bg_title_yellow);
                        holder.bg_content.setBackgroundResource(R.drawable.bg_content_yellow);
                        break;
                    case DeliveryInfo.COLOR_GREEN:
                        holder.bg_title.setBackgroundResource(R.drawable.bg_title_green);
                        holder.bg_content.setBackgroundResource(R.drawable.bg_content_green);
                        break;
                    default:
                        holder.bg_title.setBackgroundResource(R.drawable.bg_title_blue);
                        holder.bg_content.setBackgroundResource(R.drawable.bg_content_blue);
                        break;
                }
            }
            
            //holder.item_color.setBackgroundColor(color);
            holder.item_name.setText(delivery.getName());
            holder.item_expressCompany.setText(delivery.getExpComName());
            holder.item_trackingNumber.setText(delivery.getExpressNu());
            if (null == delivery.getLatestTime()){
                holder.item_trackingTime.setText(R.string.delivery_msg_noStatus);
                holder.item_trackingInfor.setText(R.string.delivery_msg_noStatus);
            }else{
                holder.item_trackingTime.setText(delivery.getLatestTime());
                holder.item_trackingInfor.setText(delivery.getLatestStatus().replaceAll(" {2,}", " "));
            }
        }
        
        return convertView;
    }
    
    


}
